package pe.com.drokasa.mirror.datagram.response;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import pe.com.drokasa.mirror.datagram.response.model.ResponseBean017;

/**
 * Clase que representa la informaci&oacute;n a ser recibida desde el sistema MIRROR como 
 * resultado de la ejecuci&oacute;n de la transacci&oacute;n 017 - Consulta de Stock por Sucursal.
 * 
 * @author Synopsis S.A
 *
 */
public class Transaction017Response extends ResponseDatagram 
{
	private static final long serialVersionUID = -6651273899394274512L;
	
	private static final int BODY_LENGTH = 980;
	
	private static final int REPETITION_LENGTH = 49;

	private List<ResponseBean017> data;
	
	private String body;

	/**
	 * @param response
	 */
	public Transaction017Response(String response)
	{
		super(response);
		this.body = StringUtils.substring(response, 41, BODY_LENGTH);
	}
	/**
	 * @return the data
	 */
	public List<ResponseBean017> getData() 
	{
		data = new ArrayList<ResponseBean017>();

		int repetition_length_counter = 0;
		for(int i = 0; i < BODY_LENGTH / REPETITION_LENGTH; i++)
		{
			String repetition = StringUtils.trim(StringUtils.substring(body, repetition_length_counter, repetition_length_counter + REPETITION_LENGTH));
			
			if(StringUtils.isNotEmpty(repetition))
			{			
				ResponseBean017 bean = new ResponseBean017();
				bean.setCodigoSucursal(StringUtils.substring(repetition, 0, 2).trim());
				bean.setNombreSucursal(StringUtils.substring(repetition, 2, 32).trim());
				bean.setSigno(StringUtils.substring(repetition, 32, 33).trim());
				String s = StringUtils.substring(repetition, 33, 49).trim();
				bean.setStockMercaderia(s.substring(0, s.length() - 3));
				data.add(bean);
			}
			
			repetition_length_counter += REPETITION_LENGTH;
		}
		return data;
	}
}